package com.coder.chen.scrollview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.coder.chen.scrollview.MyScrollView.IMyScrollListener;
import com.heima.scrollview14.R;

public class MyScrollViewActivity extends Activity {
    /** Called when the activity is first created. */
	
	private int[] ids = new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6};
	
	private MyScrollView myScrollView;
	
	private RadioGroup radioGroup;
	private LinearLayout body_layout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myScrollView = new MyScrollView(this);
        
        for (int i = 0; i < ids.length; i++) {
        	ImageView image = new ImageView(this);
        	image.setBackgroundResource(ids[i]);
        	myScrollView.addView(image);
		}
        
//        setContentView(myScrollView);
        
        View testView = getLayoutInflater().inflate(R.layout.test, null);
        myScrollView.addView(testView,2);
        
        body_layout = (LinearLayout) findViewById(R.id.body_layout);
        body_layout.addView(myScrollView);
        
        
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        for (int i = 0; i < myScrollView.getChildCount(); i++) {
        	RadioButton rbtn = new RadioButton(this);
        	radioGroup.addView(rbtn);
        	if(i==0){
        		rbtn.setChecked(true);
        	}
        	rbtn.setTag(i);
        	
        	rbtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
						myScrollView.moveToDest((Integer) buttonView.getTag());
					}
				}
			});
			
		}
        
        myScrollView.setMyScrollListener(new IMyScrollListener() {
			
			@Override
			public void moveToDest(int destId) {
				((RadioButton)radioGroup.getChildAt(destId)).setChecked(true);
			}
		});
        
    }
}